﻿using IServices;
using System;
using System.Collections.Generic;
using System.Text;
using YL.Core.Entity;
using YL.Core.Entity.Meter;

namespace RocketMQHandler
{
    public class DeviceStatusDataWorker
    {
        private readonly IMeter_DeviceDataServices deviceDataServices;
        private readonly IMeter_DeviceInfoServices deviceInfoServices;
        private readonly IMeter_DeviceStatusServices deviceStatusServices;
       
        public DeviceStatusDataWorker(IMeter_DeviceDataServices DeviceDataServices,
                                      IMeter_DeviceInfoServices DeviceInfoServices,
                                      IMeter_DeviceStatusServices DeviceStatusServices)
        {
           
            deviceDataServices = DeviceDataServices;
            deviceInfoServices = DeviceInfoServices;
            deviceStatusServices = DeviceStatusServices;
        }
        /// <summary>
        /// 更新设备当前状态
        /// </summary>
        /// <param name="device_code"></param>
        /// <param name="status"></param>
        public void UpdateDeviceStatus(string device_code,bool status)
        {
            DeviceInfo device = deviceInfoServices.QueryableToEntity(x=>x.AliDeviceName== device_code);
            if(device !=null)
            {
                DeviceStatus deviceStatus = deviceStatusServices.QueryableToEntity(x=>x.DeviceId== device.DeviceId);
                if(deviceStatus !=null)
                {
                    deviceStatus.LastTime = DateTime.Now;
                    deviceStatus.Status = status;
                    deviceStatusServices.UpdateEntity(deviceStatus);
                }
            }
        }
        /// <summary>
        /// 更新设备当前参数
        /// </summary>
        /// <param name="device_code"></param>
        /// <param name="status"></param>
        public void UpdateDeviceData(string device_code)
        {
            DeviceInfo device = deviceInfoServices.QueryableToEntity(x => x.AliDeviceName == device_code);
            if (device != null)
            {
                DeviceData deviceData = deviceDataServices.QueryableToEntity(x => x.DeviceId == device.DeviceId);
                if (deviceData != null)
                {

                    deviceDataServices.UpdateEntity(deviceData);
                }
                else//不存在则新增
                {
                    deviceData = new DeviceData();
                    
                    deviceDataServices.Insert(deviceData);
                }
            }
        }
    }
}
